﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
using System.Threading;
using System.Configuration;
using System.Text.RegularExpressions;

namespace CodeRobot.APP
{
    public partial class MainFrm : Form
    {
        delegate void SetTextCallback(string text);
        private Thread catchThread = null;

        CodeRobot.Utility.IniFile iniFile = new Utility.IniFile(Application.StartupPath + "\\config.ini");

        /// <summary>
        /// 显示实时消息到showMsg
        /// </summary>
        /// <param name="strMessage">实时消息</param>
        private void SetText(string strMessage)
        {
            if (this.showMsg.InvokeRequired)
            {
                SetTextCallback d = new SetTextCallback(SetText);
                this.Invoke(d, new object[] { strMessage });
            }
            else
            {
                this.showMsg.Text = strMessage;
            }
        }

        /// <summary>
        /// 关闭整个应用程序进程
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void KillProcess(object sender, System.ComponentModel.CancelEventArgs e)
        {
            System.Diagnostics.Process.GetCurrentProcess().Kill();
        }

        public MainFrm()
        {
            InitializeComponent();
        }

        private void MainFrm_Load(object sender, EventArgs e)
        {
            this.Closing += new CancelEventHandler(KillProcess);
            skinEngine1.SkinFile = "Calmness.ssk";

            //读取历史设置信息
            txtServer.Text = iniFile.GetString("BASE", "SERVER", "");//数据库地址
            txtDBName.Text = iniFile.GetString("BASE", "DBNAME", "");//数据库名
            txtUserID.Text = iniFile.GetString("BASE", "USERID", "");//登录数据库用户名
            txtPassword.Text = iniFile.GetString("BASE", "PASSWORD", "");//登录数据库密码
            txtCharset.Text = iniFile.GetString("BASE", "CHARSET", "");//数据库字符集
            txtProjectName.Text = iniFile.GetString("BASE", "PROJECT", "");//项目命名

            //Android
            txtAndroidPackage.Text = iniFile.GetString("ANDROID", "PACKAGE", "");//包名

        }

        private void btnStart_Click(object sender, EventArgs e)
        {
            btnStart.Enabled = false;
            btnStop.Enabled = true;

            catchThread = new Thread(new ThreadStart(DoTask));
            catchThread.Start();
        }

        private void DoTask()
        {
            CodeRobot.Utility.PublicValue.SaveMessage(Convert.ToDateTime(DateTime.Now).ToString("MM-dd HH:mm:ss") + " 任务开始...");
            this.SetText(CodeRobot.Utility.PublicValue.GetMessage());

            //数据库地址
            string strServer = txtServer.Text.Trim();
            //数据库名
            string strDBName = txtDBName.Text.Trim();
            //登录数据库用户名
            string strUserID = txtUserID.Text.Trim();
            //登录数据库密码
            string strPassword = txtPassword.Text.Trim();
            //数据库字符集
            string strCharset = txtCharset.Text.Trim();

            //Android包名
            string strAndroidPackage = txtAndroidPackage.Text.Trim();

            //存储设置
            iniFile.WriteValue("BASE", "SERVER", strServer);
            iniFile.WriteValue("BASE", "DBNAME", strDBName);
            iniFile.WriteValue("BASE", "USERID", strUserID);
            iniFile.WriteValue("BASE", "PASSWORD", strPassword);
            iniFile.WriteValue("BASE", "CHARSET", strCharset);

            iniFile.WriteValue("ANDROID", "PACKAGE", strAndroidPackage);

            string strPath = Application.StartupPath;
            string strProjectName = txtProjectName.Text.Trim();
            string strNewPath = strPath + "\\" + strProjectName;
            string strUtilityPath = strPath + "\\" + strProjectName + "\\" + strProjectName + ".Utility";
            string strModelPath = strPath + "\\" + strProjectName + "\\" + strProjectName + ".Model";
            string strDBSqlHelperPath = strPath + "\\" + strProjectName + "\\" + strProjectName + ".DBSqlHelper";
            string strDALPath = strPath + "\\" + strProjectName + "\\" + strProjectName + ".DAL";
            string strBLLPath = strPath + "\\" + strProjectName + "\\" + strProjectName + ".BLL";
            string strManagePath = strPath + "\\" + strProjectName + "\\" + strProjectName + ".Manage";//后台
            string strAPIPath = strPath + "\\" + strProjectName + "\\" + strProjectName + ".API";//RESTFUL API-APP接口文件
            string strAndroidPath = strPath + "\\" + strProjectName + "\\" + strProjectName + ".Android";//Android实体类

            if (!Directory.Exists(strNewPath))
            {
                Directory.CreateDirectory(strNewPath);
                Directory.CreateDirectory(strUtilityPath);
                Directory.CreateDirectory(strModelPath);
                Directory.CreateDirectory(strDBSqlHelperPath);
                Directory.CreateDirectory(strDALPath);
                Directory.CreateDirectory(strBLLPath);
                Directory.CreateDirectory(strManagePath);
                Directory.CreateDirectory(strAPIPath);
                Directory.CreateDirectory(strAndroidPath);

                //存储路径
                iniFile.WriteValue("DIR", "UTILITY", strUtilityPath);
                iniFile.WriteValue("DIR", "MODEL", strModelPath);
                iniFile.WriteValue("DIR", "DBSQLHELPER", strDBSqlHelperPath);
                iniFile.WriteValue("DIR", "DAL", strDALPath);
                iniFile.WriteValue("DIR", "BLL", strBLLPath);
                iniFile.WriteValue("BASE", "PROJECT", strProjectName);
                iniFile.WriteValue("DIR", "MANAGE", strManagePath);
                iniFile.WriteValue("DIR", "API", strAPIPath);
                iniFile.WriteValue("ANDROID", "MODEL", strAndroidPath);
            }

            //生成代码
            CodeRobot.DAL.CodeHelper table = new DAL.CodeHelper();
            table.GetTables(strDBName,showMsg);

            CodeRobot.Utility.PublicValue.SaveMessage("\r\n" + Convert.ToDateTime(DateTime.Now).ToString("MM-dd HH:mm:ss") + " 任务结束!");
            this.SetText(CodeRobot.Utility.PublicValue.GetMessage());

            StopTask();
        }

        private void btnStop_Click(object sender, EventArgs e)
        {
            StopTask();
        }

        private void StopTask()
        {
            btnStart.Enabled = true;
            btnStop.Enabled = false;

            catchThread.Abort();
        }
    }
}
